﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XmlxTextListNewItemWorker
	{
		
		public static void  newItem(Form form, XmlToEditContext context)
		{
			//CID 0236 //2009.03.03 //Begin
			if( context.XmlToEdit.MultiSelectListBoxRef != null  )
			{
				doNewMultiSelectListItem(form, context);
			}
			//CID 0236 //2009.03.03 //End
			else
			{
				//因为xTextList是根据上下文添加新元素的,所以只需要parseItem.这里分开处理比较好.
				XmlXmlToEditItemWorker.parseItem(context);
				doNewItem(form, context);
			}
		}
		
		//CID 0236 //2009.03.03 //Begin
		private static void doNewMultiSelectListItem( Form form, XmlToEditContext context )
		{
			string newItemValue = form.ParamTable[ JetConstant.XDOC_PARAM_MULTI_SELECT_LIST_ITEM_VALUE ] as string;
			if( newItemValue == null )
			{
				throw new InfoJetSoft.Service.InfoJetException( "Cannot find the new item value.", "XXTLNIW01" );
			}
			//CID 0546 //2010.05.11 //Begin
			string innerContextId = form.ParamTable[ JetConstant.XDOC_PARAM_MULTI_SELECT_LIST_CONTEXT_ID ] as string;
			if( innerContextId == null )
			{
				throw new InfoJetSoft.Service.InfoJetException( "Cannot find the context id.", "XXTLNIW02" );
			}
			//CID 0546 //2010.05.11 //End
			XmlNode contextNode = DOMUtil.findNodeByJetId( form, innerContextId );
			XmlNodeList itemNodeList = DOMUtil.selectNodes( contextNode, context.XmlToEdit.MultiSelectListBoxRef, 0, form.XmlNsResolver );
			if( itemNodeList.Count > 0 )
			{
				XmlElement lastItemElement = itemNodeList[ itemNodeList.Count - 1 ] as XmlElement;
				XmlElement parentElement = lastItemElement.ParentNode as XmlElement;
				XmlElement newElement = lastItemElement.CloneNode(false) as XmlElement;
				DOMUtil.setNodeValue(newElement, newItemValue);
				if (SchemaUtil.canInsertAfter(form, parentElement, newElement, lastItemElement))
				{
					parentElement.InsertAfter( newElement, lastItemElement );
					form.NewStructureNode = newElement;

					//CID 0473 //2009.12.14 //Begin
					XmlToEditUtil.domEventHandler( form, context, newElement );
                    //CID 0907 //2011.05.13 //Begin
					FormCalculatedFieldWorker.calcaulteGroupNode( form, newElement, null, false );
                    //CID 0907 //2011.05.13 //End
					//CID 0473 //2009.12.14 //End
					string path = DOMUtil.getNodePath( newElement );
					FormCodeDispatchWorker.OnAfterChange( form, path, newElement, FormCodeDispatchWorker.Updated, false );
				}
			}
			else
			{
				string itemNodeName = ExpressionUtil.parseTargetNodeName( context.XmlToEdit.Item );
				string refContextFilter = context.XmlToEdit.MultiSelectListBoxRef.Substring( 0, context.XmlToEdit.MultiSelectListBoxRef.Length - itemNodeName.Length - 1 );
				XmlElement refContextElement = DOMUtil.selectOneNode( contextNode, refContextFilter, form.XmlNsResolver ) as XmlElement;
				XmlElement newElement = form.XmlDoc.CreateElement( itemNodeName, refContextElement.NamespaceURI );
				DOMUtil.setNodeValue(newElement, newItemValue);
				ArrayList childElementList = new ArrayList();
				foreach( XmlNode childNode in refContextElement.ChildNodes )
				{
					if( childNode is XmlElement )
					{
						childElementList.Add( childNode );
					}
				}
				bool inserted = false;
				if( childElementList.Count > 0 )
				{
					foreach( XmlElement childElement in childElementList )
					{
						if (SchemaUtil.canInsertBefore(form, refContextElement, newElement, childElement))
						{
							refContextElement.InsertBefore( newElement, childElement );
							form.NewStructureNode = newElement;

							//CID 0473 //2009.12.14 //Begin
							XmlToEditUtil.domEventHandler( form, context, newElement );
                            //CID 0907 //2011.05.13 //Begin
                            FormCalculatedFieldWorker.calcaulteGroupNode( form, newElement, null, false );
                            //CID 0907 //2011.05.13 //End
							//CID 0473 //2009.12.14 //End
							string path = DOMUtil.getNodePath( newElement );
							FormCodeDispatchWorker.OnAfterChange( form, path, newElement, FormCodeDispatchWorker.Updated, false );

							inserted = true;
							break;
						}
					}
				}
				if( !inserted )
				{
					if (SchemaUtil.canAppendChild(form, refContextElement, newElement))
					{
						refContextElement.AppendChild( newElement );
						form.NewStructureNode = newElement;

						//CID 0473 //2009.12.14 //Begin
						XmlToEditUtil.domEventHandler( form, context, newElement );
                        //CID 0907 //2011.05.13 //Begin
						FormCalculatedFieldWorker.calcaulteGroupNode( form, newElement, null, false );
                        //CID 0907 //2011.05.13 //End
						//CID 0473 //2009.12.14 //End
						string path = DOMUtil.getNodePath( newElement );
						FormCodeDispatchWorker.OnAfterChange( form, path, newElement, FormCodeDispatchWorker.Updated, false );
					}
				}
			}
		}
		//CID 0236 //2009.03.03 //End

		private static void doNewItem(Form form, XmlToEditContext context)
		{
			XmlDocument doc = context.Doc;
			
			XmlNode itemNode = context.ItemNode;
			XmlNode parentNode = itemNode.ParentNode;
			XmlNode newNode = itemNode.CloneNode(false);
			XmlElement nextElement = SchemaUtil.getNextSiblingElement(itemNode);
			
			if (itemNode is XmlElement && newNode is XmlElement && parentNode is XmlElement)
			{
				XmlElement parentElement = (XmlElement) parentNode;
				XmlElement newElement = (XmlElement) newNode;
				DOMUtil.setNodeValue(newElement, "");
				XmlElement itemElement = (XmlElement) itemNode;
				
				if (nextElement != null)
				{
					if (SchemaUtil.canInsertBefore(form, parentElement, newElement, nextElement))
					{
						parentElement.InsertBefore(newElement, nextElement);
						form.NewStructureNode = newElement;

						//CID 0473 //2009.12.14 //Begin
						XmlToEditUtil.domEventHandler( form, context, newElement );
                        //CID 0907 //2011.05.13 //Begin
						FormCalculatedFieldWorker.calcaulteGroupNode( form, newElement, null, false );
                        //CID 0907 //2011.05.13 //End
                        //CID 0473 //2009.12.14 //End
						string path = DOMUtil.getNodePath( newElement );
						FormCodeDispatchWorker.OnAfterChange( form, path, newElement, FormCodeDispatchWorker.Updated, false );
					}
				}
				else
				{
					if (SchemaUtil.canAppendChild(form, parentElement, newElement))
					{
						parentElement.AppendChild(newElement);
						form.NewStructureNode = newElement;

						//CID 0473 //2009.12.14 //Begin
						XmlToEditUtil.domEventHandler( form, context, newElement );
                        //CID 0907 //2011.05.13 //Begin
						FormCalculatedFieldWorker.calcaulteGroupNode( form, newElement, null, false );
                        //CID 0907 //2011.05.13 //End
                        //CID 0473 //2009.12.14 //End
						string path = DOMUtil.getNodePath( newElement );
						FormCodeDispatchWorker.OnAfterChange( form, path, newElement, FormCodeDispatchWorker.Updated, false );
					}
				}
			}
		}
	}
}